<?php
header("Content-type: image/gif");

$grille[0][0]=8;
$grille[0][1]=0;
$grille[0][2]=27;
$grille[0][3]=35;
$grille[0][4]=41;
$grille[0][5]=0;
$grille[0][6]=63;
$grille[0][7]=0;
$grille[0][8]=0;

$grille[1][0]=0;
$grille[1][1]=12;
$grille[2][2]=0;
$grille[1][3]=31;
$grille[1][4]=0;
$grille[1][5]=56;
$grille[1][6]=0;
$grille[1][7]=76;
$grille[1][8]=80;

$grille[2][0]=0;
$grille[2][1]=18;
$grille[2][2]=24;
$grille[2][3]=0;
$grille[2][4]=44;
$grille[2][5]=0;
$grille[2][6]=62;
$grille[2][7]=78;
$grille[2][8]=0;

//Constante
$grille_x = 1000;
$grille_y = 500;
$marge = 30;
$margeCase = 5;
$GLOBALS["fontsize"] = 44;
$GLOBALS["fontfile"] = "ariblk.ttf";
//fin

$new_image = imagecreate($grille_x, $grille_y);
//settage des couleurs
$black = ImageColorAllocate($new_image, 0, 0, 0);
$white = ImageColorAllocate($new_image, 255, 255, 255);
$white2 = ImageColorAllocate($new_image, 238, 228, 196);
$blue = ImageColorAllocate($new_image, 67, 110, 159);


$card_color = $blue;

//construction grille
imagefilledrectangle($new_image,0,0,$grille_x,$grille_y,$card_color);
imagerectangle($new_image,0,0,$grille_x-1,$grille_y-1,$black);

for ($j=0;$j<=2;$j++) {
	for ($i=0;$i<=8;$i++) {
		$x_size = floor((($grille_x - ($marge * 2)) / 9)) ;
		$y_size = floor((($grille_y - ($marge * 2)) / 3)) ;
		$x_position = $marge + $i*$x_size;
		$y_position = $marge + $j*$y_size;

		imagefilledrectangle($new_image,$x_position,$y_position,$x_position+ $x_size,$y_position + $y_size,$white2);
		imagerectangle($new_image,$x_position,$y_position,$x_position+ $x_size,$y_position + $y_size,$black);

		if($grille[$j][$i]==0) {
			imagefilledrectangle($new_image,$x_position+$margeCase,$y_position+$margeCase,$x_position+ $x_size-$margeCase,$y_position + $y_size - $margeCase,$card_color);
		} elseif(is_numeric($grille[$j][$i])) {
			//petit algorithme de centrage...
			$x_position = $x_position+floor(($x_size - widthText($grille[$j][$i]))/2);
			$y_position = $y_position+floor(($y_size - heightText($grille[$j][$i]))/2)+heightText($grille[$j][$i]);
			imagettftext($new_image, $GLOBALS["fontsize"], 0, $x_position, $y_position, $black,$GLOBALS["fontfile"], $grille[$j][$i]);
		}
	}
}

imagegif($new_image);


//calcul la largeur du texte (pour centrer)
function widthText($string) {
	$args = imageftbbox($GLOBALS["fontsize"], 0, $GLOBALS["fontfile"], $string, array ("linespacing" => 0));
	return abs($args[4] - $args[0]);
}
//calcul la hauteur du texte (pour centrer)
function heightText($string) {
	$args = imageftbbox($GLOBALS["fontsize"], 0, $GLOBALS["fontfile"], $string, array ("linespacing" => 0));
	return abs($args[1] - $args[5]);
}
?>